﻿using DeHeng_Mes.Model.Table;
using DeHeng_Mes.Utils;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeHeng_Mes.Dao
{
    public class ProductionPowerDao
    {
        /// <summary>
        /// 判断当日产量是否保存
        /// </summary>
        /// <param name="name"></param>
        /// <param name="now"></param>
        /// <param name="production"></param>
        /// <returns></returns>
        public bool ProductionIsSave(string name, string date, KeyValuePair<string, string> production)
        {
            using (var db = SqlSugarConfig.Db)
            {
                return db.Queryable<TraceProduction>()
                         .Where(x => x.PlcName == name)
                         .Where(x => x.DateTime == date)
                         .Where(x => x.LR == production.Key)
                         .Any();
            }
        }

        /// <summary>
        /// 更新当日产量数据
        /// </summary>
        /// <param name="name"></param>
        /// <param name="date"></param>
        /// <param name="production"></param>
        public void UpdateProduction(string name, string date, KeyValuePair<string, string> production)
        {
            using (var db = SqlSugarConfig.Db)
            {
                db.Updateable<TraceProduction>()
                  .SetColumns(x => x.FProduction == production.Value)
                  .Where(x => x.LR == production.Key)
                  .Where(x => x.PlcName == name)
                  .Where(x => x.DateTime == date)
                  .ExecuteCommand();
            }
        }

        /// <summary>
        /// 插入数据
        /// </summary>
        /// <param name="name"></param>
        /// <param name="date"></param>
        /// <param name="production"></param>
        public void ProductionSave(string name, string date, KeyValuePair<string, string> production)
        {
            var traceProduction = new TraceProduction()
            {
                DateTime = date,
                PlcName = name,
                LR = production.Key,
                FProduction = production.Value
            };

            using (var db = SqlSugarConfig.Db)
            {
                db.Insertable(traceProduction).ExecuteCommand();
            }
        }

        /// <summary>
        /// 能耗是否保存
        /// </summary>
        /// <param name="name"></param>
        /// <param name="date"></param>
        /// <param name="power"></param>
        /// <returns></returns>
        public bool PowerIsSave(string name, string date, string power)
        {
            using (var db = SqlSugarConfig.Db)
            {
                return db.Queryable<TracePower>()
                         .Where(x => x.PlcName == name)
                         .Where(x => x.DateTime == date)
                         .Any();
            }
        }

        /// <summary>
        /// 更新能耗
        /// </summary>
        /// <param name="name"></param>
        /// <param name="date"></param>
        /// <param name="power"></param>
        public void UpdatePower(string name, string date, string power)
        {
            using (var db = SqlSugarConfig.Db)
            {
                db.Updateable<TracePower>()
                  .SetColumns(x => x.Fpower == power)
                  .Where(x => x.PlcName == name)
                  .Where(x => x.DateTime == date)
                  .ExecuteCommand();
            }
        }

        /// <summary>
        /// 存储能耗数据
        /// </summary>
        /// <param name="name"></param>
        /// <param name="date"></param>
        /// <param name="power"></param>
        public void PowerSave(string name, string date, string power)
        {
            var traceProduction = new TracePower()
            {
                DateTime = date,
                PlcName = name,
                Fpower = power
            };

            using (var db = SqlSugarConfig.Db)
            {
                db.Insertable(traceProduction).ExecuteCommand();
            }
        }

        /// <summary>
        /// 根据日期、小时、位置查询产量
        /// </summary>
        public string GetHoutProduction(TraceHourProduction traceHourProduction)
        {
            string production = null;

            using (var db = SqlSugarConfig.Db)
            {
                production = db.Queryable<TraceHourProduction>().Where(x => x.Date == traceHourProduction.Date && x.Hour == traceHourProduction.Hour && x.LR == traceHourProduction.LR)
                               .Select(x => x.FProduction)
                               .First();

                if (production == null)
                {
                    db.Insertable(traceHourProduction).ExecuteCommand();
                    return traceHourProduction.FProduction;
                }
            }

            return production;
        }

        /// <summary>
        /// 更新产量
        /// </summary>
        /// <param name="traceHourProduction"></param>
        /// <param name="realProduction"></param>
        public void UpdateHourProduction(TraceHourProduction traceHourProduction, string realProduction)
        {
            using (var db = SqlSugarConfig.Db)
            {
                db.Updateable<TraceHourProduction>().Where(x => x.Date == traceHourProduction.Date && x.Hour == traceHourProduction.Hour && x.LR == traceHourProduction.LR)
                  .SetColumns(x => new TraceHourProduction()
                  {
                      FProduction = realProduction,
                  })
                  .ExecuteCommand();
            }
        }

        /// <summary>
        /// 根据日期区间查找产量数据
        /// </summary>
        /// <param name="firstDayOfMonth"></param>
        /// <param name="lastDayOfMonth"></param>
        /// <returns></returns>
        public List<TraceHourProduction> GetProductionHour(DateTime firstDayOfMonth, DateTime lastDayOfMonth)
        {
            using (var db = SqlSugarConfig.Db)
            {
                List<TraceHourProduction> traceHourProductions = db.Queryable<TraceHourProduction>()
                                                                   .Where(x => x.Date >= firstDayOfMonth && x.Date <= lastDayOfMonth)
                                                                   .ToList();

                return traceHourProductions;
            }
        }

        /// <summary>
        /// 查询数据是否保存
        /// </summary>
        /// <param name="value"></param>
        /// <param name="today"></param>
        /// <param name="hour"></param>
        /// <param name="plcName"></param>
        /// <returns></returns>
        public bool HourPowerIsSave(double value, DateTime today, int hour, string plcName)
        {
            using (var db = SqlSugarConfig.Db)
            {
                bool isSave = db.Queryable<HourPower>()
                                .Any(x => x.Date == today && x.Hour == hour.ToString() && x.PlcName == plcName);

                if (isSave)
                {
                    return true;
                }
                else
                {
                    HourPower hourPower = new HourPower()
                    {
                        PlcName = plcName,
                        Date = today,
                        Hour = hour.ToString(),
                        Power = value.ToString()
                    };
                    db.Insertable(hourPower).ExecuteCommand();
                    return false;
                }
            }
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="value"></param>
        /// <param name="today"></param>
        /// <param name="hour"></param>
        /// <param name="plcName"></param>
        public void HourPowerUpDate(double value, DateTime today, int hour, string plcName)
        {
            using (var db = SqlSugarConfig.Db)
            {
                db.Updateable<HourPower>()
                    .Where(x => x.Hour == hour.ToString() && x.Date == today && x.PlcName == plcName)
                    .SetColumns(x => new HourPower()
                    {
                        Power = value.ToString()
                    })
                    .ExecuteCommand();
            }
        }

        /// <summary>
        /// 获取小时能耗数据
        /// </summary>
        /// <param name="today"></param>
        /// <returns></returns>
        public List<HourPower> GetHourPowerData(DateTime today)
        {
            using (var db = SqlSugarConfig.Db)
            {
                List<HourPower> hourPowers = db.Queryable<HourPower>()
                                               .Where(x => x.Date == today)
                                               .ToList();

                return hourPowers;
            }
        }
    }
}
